package com.example.hfiveandaplication.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import java.io.File;

public class ApkUtils {
    public static void install(Context context, String filePath) {
        String TAG = "ApkUtils";
        DebugLog.i(TAG, "开始执行安装: " + filePath);
        File apkFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
       /* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            DebugLog.w(TAG, "版本大于 N ，开始使用 fileProvider 进行安装");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    mContext
                    , "你的包名.fileprovider"
                    , apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {*/
        DebugLog.w(TAG, "正常进行安装");
        intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
//        }
        context.startActivity(intent);
    }


    /**
     * @param activity
     * @param appPkg  应用的包名
     */
    public static void launchAppDetail(Activity activity, String appPkg) {
        final String GOOGLE_PLAY = "com.android.vending";//谷歌商店
        try {
            if (TextUtils.isEmpty(appPkg))
                return;
            Uri uri = Uri.parse("market://details?id=" + appPkg);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            intent.setPackage(GOOGLE_PLAY);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
        } catch (Exception e) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri content_url = Uri.parse("https://play.google.com/store/apps/details?id="+appPkg);
            intent.setData(content_url);
            activity.startActivity(intent);
        }
    }
}
